package com.yzt.loginmodule.select.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.yzt.loginmodule.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CountrySortAdapter extends BaseAdapter implements SectionIndexer {

    private List<CountrySortModel> mList;

    private final Context mContext;

    private final boolean isSelectCountry;

    /**
     * 初始化
     */
    public CountrySortAdapter(Context mContext, List<CountrySortModel> list, boolean isSelectCountry) {
        this.mContext = mContext;
        if (list == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = list;
        }
        this.isSelectCountry = isSelectCountry;
    }

    public void setDefaultCountry(CountrySortModel country) {
        mList.add(country);
    }

    @Override
    public int getCount() {
        return this.mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder;
        final CountrySortModel mContent = mList.get(position);

        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_country_zone, null);
            viewHolder.countrySortName = view.findViewById(R.id.country_catalog);
            viewHolder.countryName = view.findViewById(R.id.country_name);
            viewHolder.countryNumber = view.findViewById(R.id.country_number);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section) && !"!".equals(mContent.sortLetters)) {
            viewHolder.countrySortName.setVisibility(View.VISIBLE);
            viewHolder.countrySortName.setText(mContent.sortLetters);
        } else {
            viewHolder.countrySortName.setVisibility(View.GONE);
        }

        viewHolder.countryName.setText(this.mList.get(position).countryName);
        viewHolder.countryNumber.setText(this.mList.get(position).countryNumber);

        if (isSelectCountry) {
            viewHolder.countryNumber.setVisibility(View.GONE);
        }

        return view;
    }

    @Override
    public int getPositionForSection(int section) {
        if (section != 42) {
            for (int i = 0; i < getCount(); i++) {
                String sortStr = mList.get(i).sortLetters;
                char firstChar = sortStr.toUpperCase(Locale.CHINESE).charAt(0);
                if (firstChar == section) {
                    return i;
                }
            }
        } else {
            return 0;
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return mList.get(position).sortLetters.charAt(0);
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     */
    public void updateListView(List<CountrySortModel> list) {
        if (list == null) {
            this.mList = new ArrayList<>();
        } else {
            this.mList = list;
        }
        notifyDataSetChanged();
    }

    public static class ViewHolder {
        /**
         * 国家码简拼所属的字母范围
         */
        public TextView countrySortName;

        /**
         * 国家名
         */
        public TextView countryName;

        /**
         * 代码
         */
        public TextView countryNumber;
    }
}
